﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 单例基类
///  作用  ：继承后变成单例类
/// </summary>
/// <typeparam name="T">作用类</typeparam>
/// /// <summary>
/// 泛型约束
/// 1.class：引用类型
/// 2.struct：值类型
/// 3.基类（类）：T是基类的子类
/// 4.接口：T具有接口的方法
/// 5.new():T具有构造函数
/// </summary>
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
    private static T instance;
    public static T Instance
    {
        get
        {
            return instance;
        }
    }

    protected virtual void Awake()
    {
        if (instance == null)
        {
            instance = this as T;
            DontDestroyOnLoad(this.gameObject);
        }
        else
        {
            Destroy(this.gameObject);
        }
    }
}
